<?php
require_once("../lib/func.autoloader.php");
// users.php
// SuperConstructor users page (admin-only)
// Copyright 2007, Alex McClung

// ===============================================================================
// DEFINES
define('MODE_ADD',    'add');
define('MODE_EDIT',   'edit');
define('MODE_REMOVE', 'remove');
define('MODE_PORTAL', 'portal');

// ===============================================================================
// INIT

// If the user is not logged in, redirect them to the login page
// TODO: Make sure they are an administrator
$sc = new SuperConstructor();
if(false === $sc->getUser()) { header("Location: {$sc->config->LoginPage}"); exit(); }

// Get mode (from GET) (default to portal mode)
$mode = $_GET['mode'];

// Reserve strings
$tpl          = ""; // Template file to display
$title        = ""; // Page title
$func         = ""; // Function to perform on the DBO (Insert, Update, or Remove)
$message_ok   = ""; // Success message
$message_fail = ""; // Failure message

// Create the DBO instance that gets tossed around
$user = new SC_User();

// If POST is set, load it into the object
if(count($_POST) > 0)
	$sc->LoadFormData($_POST, $user);
// If the ID is set in GET (like when we're in edit or remove mode), initialize
// the object with it.
else if(isset($_GET['id']))
	$user->Get($_GET['id']);

// ===============================================================================
// MAIN EXECUTION

// Figure out things based on which mode we're in
switch($mode)
{
	case MODE_ADD:
			$tpl          = "add.tpl";
			$title        = "[PH]ADD";
			$func         = "Insert";
			$message_ok   = "Successfully created <strong>{$user->name}</strong>.";
			$message_fail = "Error creating a new user.";
		break;
	
	case MODE_EDIT:
			$tpl          = "edit.tpl";
			$title        = "[PH]EDIT";
			$func         = "Update";
			$message_ok   = "Successfully edited <strong>{$user->name}</strong>";
			$message_fail = "Error editing <strong>{$user->name}</strong>";
		break;
	
	case MODE_REMOVE:
			$tpl          = "remove.tpl";
			$title        = "[PH]REMOVE";
			$func         = "Remove";
			$message_ok   = "Successfully removed <strong>{$user->name}</strong>";
			$message_fail = "Error removing <strong>{$user->name}</strong>";
		break;
	
	case MODE_PORTAL:
	default:
			$mode  = MODE_PORTAL; // Make sure mode is initialized with a value
			$tpl   = "index.tpl";
			$title = "[PH]USERS";
		break;
}
// If we're NOT in portal mode, we need to give the template the list of possible user types to show in the form
if($mode != MODE_PORTAL)
{
	// TODO: Get these from the SC_User object somehow
	$sc->assign("_types", array("Content Editor", "Administrator"));
}

// Assign common template variables (it is important to do this before the next step)
$sc->assign("pageTitle",  $title );
$sc->assign("mode",       $mode  );
$sc->assign_by_ref("user", $user );

// Is there an operation to perform on our little object?
if(strlen($func) > 0 && count($_POST) > 0 && $mode != MODE_PORTAL)
{
	// If there are no error messages, we can try and update the database
	if( MQ::NumMessages(MQ_ERROR) == 0 )
	{
		// Try and do something
		if($user->$func())
		{
			// Tell them how awesome they are
			MQ::Push($message_ok, MQ_OK);
			
			// Clear form data ONLY if we're in add mode
			if($mode == MODE_ADD)
				$sc->clear_assign("user");
			// And if we're in remove mode, make sure we don't show the submit button again
			else if($mode == MODE_REMOVE)
				$sc->assign("removed", true);
		}
		else
		{
			// Tell them how badly they failed.
			MQ::Push($message_fail, MQ_ERROR);
			MQ::Push($user->LastError(), MQ_ERROR);
		}
	}
}
// Are we in portal mode?
else if($mode == MODE_PORTAL)
{
	// Load the existing maps
	$user   = new SC_User();
	$search = new SimpleDBOSearch($user);
	$result = $search->Search();
	$rows   = $result->All($user);
	
	// Cleanup
	unset($user, $s, $result, $search);
	
	// Assign template variables
	$sc->assign_by_ref("rows", $rows);
}

// Cleanup
unset($title, $func, $message_ok, $message_fail, $loadtables);

// TEMPLATE! DISPLAY!
$sc->display("admin/pages/users/$tpl");
?>